The DICTIONARY function creates a new dictionary. An IDL dictionary is a compound data type that contains key-value pairs of different data types including any mixture of scalars, arrays, structures, pointers, object references, lists, hashes, and other dictionaries. Unlike HASH, the keys in a dictionary are case insensitive and must be valid IDL variable names. An IDL dictionary is very similar to an IDL structure except that it is easy to add or remove keys, or change the data type of a value.
IDL dictionaries have the following properties:
Note: Since the DICTIONARY is so similar to HASH, most of the documentation can be found under HASH. Here we provide some examples of the differences between the two data types.
dict = DICTIONARY("one", 1.0, "blue", [255,0,0], "Pi", !DPI)
PRINT, N_ELEMENTS(dict)
IDL prints:
3
dict = DICTIONARY("one", 1.0, "blue", [255,0,0], "Pi", !DPI)
PRINT, dict["one"]
PRINT, dict.one
PRINT, dict.ONE
In all three cases IDL prints:
1.00000
Now try changing the data type:
dict.one = 'my value'
PRINT, dict.One ; case insensitive
IDL prints:
my value
Now dynamically add a new key:
dict.newkey = [0,1,2]
PRINT, dict.newkey
IDL prints:
0 1 2
keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
values = LIST('one', 2.0, 3, 4l, PTR_NEW(5), {n:6}, COMPLEX(7,0))
dict = DICTIONARY(keys, values)
PRINT, N_ELEMENTS(dict)
IDL prints:
7
struct = {FIELD1: 4.0, FIELD2: {SUBFIELD1: "hello", SUBFIELD2: 3.14}}
dict = DICTIONARY(struct, /EXTRACT)
PRINT, dict
PRINT, dict.field2
PRINT, 'subfield1 = ', dict.field2.subfield1
IDL prints:
FIELD2: DICTIONARY <ID=4 NELEMENTS=2>
FIELD1: 4.00000
SUBFIELD1: hello
SUBFIELD2: 3.14000
subfield1 = hello
For details on the input arguments and keywords see HASH.
Result = DICTIONARY( Key1, Value1, Key2, Value2, ... Keyn, Valuen, /EXTRACT , /NO_COPY )
or
Result = DICTIONARY( Keys, Values, /EXTRACT )
or
Result = DICTIONARY( Keys )
or
Result = DICTIONARY( Structure, /EXTRACT )
See Hash::Count for detailed documentation.
Result = dictionary.Count( [Value] )
See Hash::Filter for detailed documentation.
Result = dictionary.Filter(Function, Args)
See Hash::HasKey for detailed documentation.
Result = dictionary.HasKey( Keys )
See Hash::IsEmpty for detailed documentation.
Result = dictionary.IsEmpty( )
See Hash::Keys for detailed documentation.
Result = dictionary.Keys( )
See Hash::Map for detailed documentation.
Result = dictionary.Map(Function, Args)
See Hash::Reduce for detailed documentation.
Result = dictionary.Reduce(Function, Args, VALUE=value)
See Hash::Remove for detailed documentation.
dictionary.Remove [, Keys] [, /ALL]
or
Result = dictionary.Remove( [, Keys] [, /ALL] )
See Hash::ToStruct for detailed documentation.
Result = dictionary.ToStruct( [, MISSING=value] [, /NO_COPY] [, /RECURSIVE] [, SKIPPED=variable] )
See Hash::Values for detailed documentation.
Result = dictionary.Values( )
See Hash::Where for detailed documentation.
Result = dictionary.Where( Value [, COMPLEMENT=variable] [, COUNT=variable] [, NCOMPLEMENT=variable] )
See the following sections in HASH for additional information on using dictionaries:
In many cases, you can access elements of a dictionary variable using standard IDL array syntax, as if the dictionary were a one-dimensional array. You can also access elements using standard IDL structure syntax using dot notation.
To copy the value of a single dictionary element into a new variable, leaving the dictionary unchanged, use array syntax:
value = dictionary[Key]
where Key is an IDL variable containing a scalar string.
Or you can use dot notation:
value = dictionary.Key
where Key is the name of the desired element within the dictionary.
For example:
d = DICTIONARY("planet", "Saturn")
mykey = "dwarf"
; Three ways to access the same key.
PRINT, d[mykey], d["dwarf"], d.dwarf
Note: Using array notation, you are supplying a variable containing a string key. Using dot notation, you are hard-coding the key name into your program. The array notation method is more flexible because the variable can be created at runtime, while the dot notation may produce more human-readable code and is slightly faster to execute.
Note: IDL structures have a "special" parentheses syntax where you can retrieve structure fields by the tag number enclosed in parentheses. You cannot use this parentheses notation with dictionaries.
To insert a single value into a dictionary, use array syntax or dot notation:
dictionary[Key] = Value
or
dictionary.Key = Value
where Value is the value to be stored in the new dictionary element.
Note: Using array notation, you supply a variable containing a string key. Using dot notation, you hard-code the key name into your program. The array notation method is more flexible because the variable can be created at runtime, while the dot notation may produce more human-readable code and is slightly faster to execute.
To change the value of a single dictionary element, use array syntax or dot notation:
dictionary[Key] = Value
or
dictionary.Key = Value
where Value is the new value.
Note: Using array notation, you supply a variable containing a string key. Using dot notation, you hard-code the key name into your program. The array notation method is more flexible because the variable can be created at runtime, while the dot notation may produce more human-readable code and is slightly faster to execute.
If your dictionary contains an array, you can combine the dot notation with brackets to access a subset of the array. For example:
IDL> dict = DICTIONARY('data', FINDGEN(10))
IDL> PRINT, dict.data[2:5]
2.00000 3.00000 4.00000 5.00000
However, unlike structures, you cannot change the array elements using the dot notation:
IDL> dict = DICTIONARY('data', FINDGEN(10))
IDL> dict.data[2:5] = 0
% Attempt to store into an expression: Structure reference.
% Execution halted at: $MAIN$
Instead, you should use the bracket notation with multiple indexing to change just a subset. For example:
IDL> dict['data', 2:5] = 0
This is because the DICTIONARY is implemented as a container of data pointers instead of a structure that is laid out directly into memory.
Just like HASH, you can use the FOREACH operator to iterate over the dictionary.
Note: While iterating through a dictionary you should avoid adding or removing elements. If the dictionary is changed during the FOREACH, the behavior is undefined.
|
8.3 |
Introduced |
| 8.4 |
Added Filter, Map, Reduce methods |
!NULL, Creating and Defining Structures, HASH, LIST, ORDEREDHASH, Logical Operators, Modifying Object Properties, Relational Operators, Structure References, LAMBDA